﻿using System;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using IceCS.GoodsSale.BLL;
using IceCS.GoodsSale.Models.UserModels;

namespace IceCS.GoodsSale.Winform
{
    public partial class FrmGetPassword : Form
    {
        public FrmGetPassword()
        {
            InitializeComponent();
        }

        private void FrmGetPassword_Load(object sender, EventArgs e)
        {
            txtNewPassword1.Enabled = false;
            txtNewPassword2.Enabled = false;
            btnSetPassword.Enabled = false;
        }

        private void btnGetPassword_Click(object sender, EventArgs e)
        {
            string userName = txtUserName.Text.Trim();
            string regexUser = @"^[a-zA-Z]\w{4,17}$";
            if (!Regex.IsMatch(userName, regexUser))
            {
                MessageBox.Show("你输入的用户名格式不正确，请重新输入", "提示");
                txtUserName.Text = "";
                txtUserName.Focus();
                return;
            }
            UserManager userManager = new UserManager();
            if (!userManager.UserIsExist(userName))
            {
                MessageBox.Show("你输入的用户名不存在", "提示");
                txtUserName.Text = "";
                txtUserName.Focus();
                return;
            }
            if (txtName.Text.Trim() == "")
            {
                MessageBox.Show("请输入您的姓名", "提示");
                txtName.Focus();
                return;
            }
            if (txtHometown.Text.Trim() == "")
            {
                MessageBox.Show("请输入您的出生地", "提示");
                txtHometown.Focus();
                return;
            }
            string regexPhoneNumber = @"^\d{11}$";
            if (!Regex.IsMatch(txtPhoneNumber.Text.Trim(), regexPhoneNumber))
            {
                MessageBox.Show("您输入的手机号码格式不正确，请重新输入", "提示");
                txtPhoneNumber.Text = "";
                txtPhoneNumber.Focus();
                return;
            }
            string regexEmail = @"\w{1,}@\w{1,}\.";
            if (!Regex.IsMatch(txtEMail.Text.Trim(), regexEmail))
            {
                MessageBox.Show("E-Mail格式不正确，请重新输入", "提示");
                txtEMail.Text = "";
                txtEMail.Focus();
                return;
            }
            UserPswProInfoManager userPswProInfoManager = new UserPswProInfoManager();
            UserPswProInfo userPswProInfo = userPswProInfoManager.GetUserPswProInfo(userName);
            if (txtName.Text.Trim() != userPswProInfo.Name)
            {
                MessageBox.Show("您输入的姓名不正确", "提示");
                txtName.Text = "";
                txtName.Focus();
            }
            else if (txtHometown.Text.Trim() != userPswProInfo.Hometown)
            {
                MessageBox.Show("您输入的出生地不正确", "提示");
                txtHometown.Text = "";
                txtHometown.Focus();
            }
            else if (txtPhoneNumber.Text.Trim() != userPswProInfo.PhoneNumber.Trim())
            {
                MessageBox.Show("您输入的手机号码不正确", "提示");
                txtPhoneNumber.Text = "";
                txtPhoneNumber.Focus();
            }
            else if (txtEMail.Text.Trim() != userPswProInfo.EMail)
            {
                MessageBox.Show("请输入您的E-Mail地址不正确", "提示");
                txtEMail.Text = "";
                txtEMail.Focus();
            }
            else
            {
                MessageBox.Show("您输入的密码保护信息正确，请设置您的密码", "提示");
                txtNewPassword1.Enabled = true;
                txtNewPassword2.Enabled = true;
                btnSetPassword.Enabled = true;
                txtNewPassword1.Focus();
            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            txtUserName.Text = "";
            txtName.Text = "";
            txtHometown.Text = "";
            txtPhoneNumber.Text = "";
            txtEMail.Text = "";
            txtUserName.Focus();
        }

        private void btnSetPassword_Click(object sender, EventArgs e)
        {
            try
            {
                UserManager.userInfo[0] = txtUserName.Text.Trim();
                if (txtNewPassword1.Text.Trim() == "")
                {
                    MessageBox.Show("新密码不能为空，请输入", "提示");
                    txtNewPassword1.Focus();
                    return;
                }
                if (txtNewPassword2.Text.Trim() == "")
                {
                    MessageBox.Show("新密码不能为空，请输入", "提示");
                    txtNewPassword2.Focus();
                    return;
                }
                if (txtNewPassword1.Text.Trim() != txtNewPassword2.Text.Trim())
                {
                    MessageBox.Show("两次输入的新密码不一致，请重新输入", "提示");
                    txtNewPassword1.Text = "";
                    txtNewPassword2.Text = "";
                    txtNewPassword1.Focus();
                    return;
                }
                string userName = UserManager.userInfo[0];
                string userPassword = txtNewPassword1.Text.Trim();
                UserManager userManager = new UserManager();
                if (userManager.ChangeUserPassword(userName, userPassword))
                {
                    MessageBox.Show("密码设置成功，请记住您的新密码", "提示");
                    txtNewPassword1.Text = "";
                    txtNewPassword2.Text = "";
                    txtNewPassword1.Enabled = false;
                    txtNewPassword2.Enabled = false;
                    btnSetPassword.Enabled = false;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtNewPassword2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnSetPassword_Click(sender, e);
            }
        }
    }
}
